/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ThreadClient;

import Packets.pkValue;
import StaticClass.ArrSocket;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPOutputStream;
import javax.swing.JOptionPane;
//import sun.java2d.pipe.ValidatePipe;

/**
 *
 * @author Oho
 */
public class ThreadFile extends Thread {

    Socket sk = null;
    int port;
    private DataInputStream clientData;

    public ThreadFile(int port) {
        this.port = port;
    }

    public ThreadFile(Socket sk) {
        this.sk = sk;
    }
    ObjectInputStream inp = null;
    ObjectOutputStream outp = null;

    public void ThreadWrite(String text) throws IOException {
        if (outp == null) {
            outp = new ObjectOutputStream(sk.getOutputStream());
        }
        File myFile = new File(text);
        byte[] mybytearray = new byte[(int) myFile.length()];
        FileInputStream fis = new FileInputStream(myFile);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(mybytearray, 0, mybytearray.length);
        pkValue pk = new pkValue();
        pk.setCommand(2);
        pk.setValue(1);
        pk.setObj(myFile.getName());
        outp.writeObject(pk);
        pk = new pkValue();
        pk.setCommand(2);
        pk.setValue(1);
        pk.setObj(mybytearray);
        outp.writeObject(pk);
        outp.flush();
    }
 public void ThreadRead1() throws FileNotFoundException, IOException, IOException, ClassNotFoundException {
      clientData = new DataInputStream(sk.getInputStream());
      String name = clientData.readUTF();
      long size = clientData.readLong();
      
        int bytesRead;
        FileOutputStream output = new FileOutputStream(name);
            byte[] buffer = new byte[1024];
            while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int) Math.min(buffer.length, size))) != -1) {
                output.write(buffer, 0, bytesRead);
                size -= bytesRead;
            }
            output.flush();
            output.close();
//            byte[] mybytearray = (byte[]) vl.getObj();
//            bos.write(mybytearray, 0, mybytearray.length);
//            bos.flush();
//            long end = System.currentTimeMillis();
           // System.out.println(end-start);
            System.out.println("da goi xong");

    }
    public void ThreadRead() throws FileNotFoundException, IOException, IOException, ClassNotFoundException {
        if (inp == null) {
            inp = new ObjectInputStream(sk.getInputStream());
        }
        pkValue name = (pkValue) inp.readObject();


       
            pkValue vl = (pkValue) inp.readObject();
            int filesize = 6022386; // filesize temporary hardcoded
            long start = System.currentTimeMillis();
            int bytesRead;
            int current = 0;
            FileOutputStream fos = new FileOutputStream(name.getObj().toString());
            BufferedOutputStream bos = new BufferedOutputStream(fos);
            byte[] mybytearray = (byte[]) vl.getObj();
            bos.write(mybytearray, 0, mybytearray.length);
            bos.flush();
            long end = System.currentTimeMillis();
            System.out.println(end-start);
            System.out.println("da goi xong");

    }

    @Override
    public void run() {


        while (true) {
            try {
                try {
                    //while (true) {
                    //
                    //

                    ThreadRead1();
                    // inp.close();
                    //} catch (FileNotFoundException ex) {
                    //  Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
                    ///} catch (IOException ex) {
                    //
                    //}



                    //}
                } catch (ClassNotFoundException ex) {
                    Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
                }
            } catch (FileNotFoundException ex) {
                Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IOException ex) {
                Logger.getLogger(ThreadFile.class.getName()).log(Level.SEVERE, null, ex);
            }
        }


    }
}
